/*
 * Copyright (C) 2013 AdminsProL2
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.l2jserver.gameserver.instancemanager.votereward;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;

import com.l2jserver.Config;
import com.l2jserver.gameserver.Announcements;
import com.l2jserver.gameserver.ThreadPoolManager;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;

/**
 * @author fissban
 */

public class VoteRewardGlobal
{
	static Logger _log = Logger.getLogger(VoteRewardGlobal.class.getName());
	
	static final int initialCheck = Config.VOTE_SYSTEM_START_TIME * 1000;
	static final int delayForCheck = Config.VOTE_SYSTEM_CHECK_TIME * 1000;
	int votesneed;
	
	static List<String> _ips = new ArrayList<>();
	static List<String> _accounts = new ArrayList<>();
	
	static int lastVoteCount = 0;
	
	VoteRewardGlobal()
	{
		if (Config.VOTE_SYSTEM_DATABASE_SAVE)
		{
			if (Config.VOTE_SYSTEM_BRASIL)
			{
				VoteRewardBrasil.getInstance().load();
			}
			if (Config.VOTE_SYSTEM_HOPZONE)
			{
				VoteRewardHopzone.getInstance().load();
			}
			if (Config.VOTE_SYSTEM_TOPZONE)
			{
				VoteRewardTopzone.getInstance().load();
			}
			if (Config.VOTE_SYSTEM_ADMINSPRO)
			{
				VoteRewardTopzone.getInstance().load();
			}
		}
		
		ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new AutoReward(), initialCheck, delayForCheck);
	}
	
	public class AutoReward implements Runnable
	{
		@Override
		public void run()
		{
			int votos = getVotes();
			
			_log.info("VoteReward: Current votes " + votos);
			
			if (votos >= (getLastVoteCount() + Config.VOTE_SYSTEM_COUNT))
			{
				Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().valueCollection();
				{
					for (L2PcInstance onlinePlayer : pls)
					{
						if (onlinePlayer.isOnline() && !onlinePlayer.getClient().isDetached() && !_accounts.contains(onlinePlayer.getAccountName()) && !_ips.contains(onlinePlayer.getClient().getConnection().getInetAddress().getHostAddress()))
						{
							String[] parase = Config.VOTE_SYSTEM_ITEM_ID.split(",");
							String[] parase3 = Config.VOTE_SYSTEM_ITEM_COUNT.split(",");
							
							for (int o = 0; o < parase.length; o++)
							{
								int parase2 = Integer.parseInt(parase[o]);
								int parase4 = Integer.parseInt(parase3[o]);
								
								onlinePlayer.addItem("vote_reward", parase2, parase4, onlinePlayer, true);
								_log.info(onlinePlayer.getAccountNamePlayer() + " has been rewarded for voting");
							}
							
							_ips.add(onlinePlayer.getClient().getConnection().getInetAddress().getHostAddress());
							_accounts.add(onlinePlayer.getAccountName());
						}
					}
				}
				
				_log.info("AutoVoteReward Global: All players has been rewared!");
				
				Announcements.getInstance().announceToAll("be rewarded!");
				Announcements.getInstance().announceToAll("for vote!");
				setLastVoteCount(getLastVoteCount() + Config.VOTE_SYSTEM_COUNT);
			}
			
			if (getLastVoteCount() == 0)
			{
				setLastVoteCount(votos);
			}
			
			else if ((((getLastVoteCount() + Config.VOTE_SYSTEM_COUNT) - votos) > Config.VOTE_SYSTEM_COUNT) || (votos > (getLastVoteCount() + Config.VOTE_SYSTEM_COUNT)))
			{
				setLastVoteCount(votos);
			}
			
			votesneed = (getLastVoteCount() + Config.VOTE_SYSTEM_COUNT) - votos;
			
			if (votesneed == 0)
			{
				votesneed = Config.VOTE_SYSTEM_COUNT;
			}
			
			Announcements.getInstance().announceToAll("== VoteReward ==");
			Announcements.getInstance().announceToAll("Contamos con: " + votos + " totales.");
			if (Config.VOTE_SYSTEM_HOPZONE)
			{
				Announcements.getInstance().announceToAll("Current votes " + VoteRewardHopzone.getInstance().getVotes() + " in Hopzone.");
			}
			if (Config.VOTE_SYSTEM_TOPZONE)
			{
				Announcements.getInstance().announceToAll("Current votes " + VoteRewardTopzone.getInstance().getVotes() + " in Topzone.");
			}
			if (Config.VOTE_SYSTEM_BRASIL)
			{
				Announcements.getInstance().announceToAll("Current votes " + VoteRewardBrasil.getInstance().getVotes() + " in TopBrasil.");
			}
			if (Config.VOTE_SYSTEM_ADMINSPRO)
			{
				Announcements.getInstance().announceToAll("Current votes " + VoteRewardAdminsPro.getInstance().getVotes() + " in Top AdminsPro.");
			}
			Announcements.getInstance().announceToAll("Missing " + votesneed + " votes!");
			
			_ips.clear();
			_accounts.clear();
		}
	}
	
	public int getVotes()
	{
		int votos = 0;
		if (Config.VOTE_SYSTEM_BRASIL)
		{
			votos += VoteRewardBrasil.getInstance().getVotes();
		}
		if (Config.VOTE_SYSTEM_HOPZONE)
		{
			votos += VoteRewardHopzone.getInstance().getVotes();
		}
		if (Config.VOTE_SYSTEM_TOPZONE)
		{
			votos += VoteRewardTopzone.getInstance().getVotes();
		}
		if (Config.VOTE_SYSTEM_ADMINSPRO)
		{
			votos += VoteRewardAdminsPro.getInstance().getVotes();
		}
		
		return votos;
	}
	
	public void setLastVoteCount(int voteCount)
	{
		lastVoteCount = voteCount;
	}
	
	int getLastVoteCount()
	{
		return lastVoteCount;
	}
	
	public void save()
	{
		if (Config.VOTE_SYSTEM_BRASIL)
		{
			VoteRewardBrasil.getInstance().save();
		}
		if (Config.VOTE_SYSTEM_HOPZONE)
		{
			VoteRewardHopzone.getInstance().save();
		}
		if (Config.VOTE_SYSTEM_TOPZONE)
		{
			VoteRewardTopzone.getInstance().save();
		}
		if (Config.VOTE_SYSTEM_ADMINSPRO)
		{
			VoteRewardAdminsPro.getInstance().save();
		}
		
	}
	
	public static VoteRewardGlobal getInstance()
	{
		return SingletonHolder._instance;
	}
	
	static class SingletonHolder
	{
		protected static final VoteRewardGlobal _instance = new VoteRewardGlobal();
	}
}
